package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.dto.UserListQryDto;
import com.bsh.mybatis.dataView.UserInfoExt;
import com.bsh.mybatis.model.User;
import com.bsh.repository.DaoAccount;
import com.bsh.repository.DaoUser;
import com.bsh.service.UserService;
import com.bsh.utils.DesUtils;
import com.bsh.utils.Pager;

/**
 * 用户
 * @author gsh
 *
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
	private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private DaoUser userDao;
	@Autowired
	private DaoAccount daoAcc;

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#getUser(java.lang.Integer)
	 */
	@Override
	public UserInfoExt getUser(Integer userid) {
		return userDao.getUser(userid);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#addUser(com.bsh.mybatis.model.User)
	 */
	@Override
	public Integer addUser(UserInfoExt user) {
		//判断name 和 mobile 是否注册过
		User userCheckMobile = this.getUserByMobile(user.getMobile());		
		if ( userCheckMobile !=null ) {
			return -101;
		}
		
		User userCheckName = this.checkUserByNameOrMobile(user.getName());		
		if (userCheckName != null ) {
			return -102;
		}
		
		//检查推荐人
		if(!StringUtils.isEmpty(user.getpName())){
			User puser = userDao.selectByName(user.getpName());
			if (puser == null)
				return -100;
			user.setPid(puser.getId());
		}
		
		DesUtils des=new DesUtils();
		String enPassword=des.encryptPassword(user.getPassword());
		user.setPassword(enPassword);
		//创建帐号
		int res = userDao.addUser(user);
		if(res>0){			
			//账户初使化
			Integer accountId = daoAcc.initUserAcount(user.getId());
			log.info(String.format("创建新用户：%s成功，用户id:%s,用户帐号：%s ", user.getName(),user.getId(),accountId));
		}
		return res;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#editUser(com.bsh.mybatis.model.User)
	 */
	@Override
	public Integer editUser(UserInfoExt user) {
		if(!StringUtils.isEmpty(user.getpName())){
			User puser = userDao.selectByName(user.getpName());
			if (puser == null)
				return -100;
			user.setPid(puser.getId());
		}
		
		return userDao.editUser(user);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#enableUser(int, boolean)
	 */
	@Override
	public Integer enableUser(int id, boolean isEnabele) {
		return userDao.enableUser(id, isEnabele ? 1 : 0);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#disableBatch(int[])
	 */
	@Override
	public Integer disableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			userDao.enableUser(id, 0);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#enableBatch(int[])
	 */
	@Override
	public Integer enableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			userDao.enableUser(id, 1);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#deleteUser(int)
	 */
	@Override
	public Integer deleteUser(int id) {
		return userDao.deleteByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#getPage(com.bsh.utils.Pager, com.bsh.dto.UserListQryDto)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,UserListQryDto qryParam) {
		StringBuilder where=new StringBuilder();
		if(qryParam!=null){
			if(!StringUtils.isEmpty(qryParam.getId())){
				where.append(String.format(" and u.id=%s ",qryParam.getId()));
			}
			if(!StringUtils.isEmpty(qryParam.getLevel()) && !"-1".equals(qryParam.getLevel())){
				where.append(String.format(" and u.level='%s' ",qryParam.getLevel().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getName())){
				where.append(String.format(" and u.name='%s' ",qryParam.getName().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getRealName())){
				where.append(String.format(" and u.real_name like '%%%s%%' ",qryParam.getRealName().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getMobile())){
				where.append(String.format(" and u.mobile='%s' ",qryParam.getMobile().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and u.create_time >='%s' ",qryParam.getCreateTime().trim()));
			}else if(StringUtils.isEmpty(qryParam.getCreateTime()) && !StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and u.create_time <='%s 23:59:59' ",qryParam.getCreateTime1().trim()));
			}else if(!StringUtils.isEmpty(qryParam.getCreateTime()) && !StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and (u.create_time between '%s' and '%s 23:59:59') ",qryParam.getCreateTime().trim(),qryParam.getCreateTime1().trim()));
			}
		}
		List<Map<String, Object>> list = userDao.getPage(pager,where.toString());
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getPageExport(UserListQryDto qryParam) {
		StringBuilder where=new StringBuilder();
		if(qryParam!=null){
			if(!StringUtils.isEmpty(qryParam.getId())){
				where.append(String.format(" and u.id=%s ",qryParam.getId()));
			}
			if(!StringUtils.isEmpty(qryParam.getLevel()) && !"-1".equals(qryParam.getLevel())){
				where.append(String.format(" and u.level='%s' ",qryParam.getLevel().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getName())){
				where.append(String.format(" and u.name='%s' ",qryParam.getName().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getMobile())){
				where.append(String.format(" and u.mobile='%s' ",qryParam.getMobile().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and u.create_time >='%s' ",qryParam.getCreateTime().trim()));
			}else if(StringUtils.isEmpty(qryParam.getCreateTime()) && !StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and u.create_time <='%s 23:59:59' ",qryParam.getCreateTime1().trim()));
			}else if(!StringUtils.isEmpty(qryParam.getCreateTime()) && !StringUtils.isEmpty(qryParam.getCreateTime1())){
				where.append(String.format(" and (u.create_time between '%s' and '%s 23:59:59') ",qryParam.getCreateTime().trim(),qryParam.getCreateTime1().trim()));
			}
		}
		List<Map<String, Object>> list = userDao.getPageExport(where.toString());
		return list;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#login(java.lang.String, java.lang.String)
	 */
	@Override
	public User login(String loginName, String password) {
		DesUtils des=new DesUtils();
		String enPassword=des.encryptPassword(password);
		User admin = this.userDao.getLoginUser(loginName, enPassword);
		return admin;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#changePassword(int, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean changePassword(int id, String password, String newPassword) {
		DesUtils des=new DesUtils();
		String encPassword=des.encryptPassword(password);
		String encNewPassword=des.encryptPassword(newPassword);
		int res = this.userDao.updatePassword(id, encPassword, encNewPassword);
		return res == 1;
	}
	
	@Override
	public boolean etlChangPassword(int id, String password, String newPassword, String type) {
		DesUtils des=new DesUtils();
//		String encPassword=des.encrypt(password);  //jane为了导数据
		String encNewPassword=des.encrypt(newPassword);
		int res = this.userDao.updatePassword(id, password, encNewPassword);
		return res == 1;
	}
	

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#resetPassword(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public boolean resetPassword(String mobile, String password, String smsVerifyCode) {
		int res = this.userDao.resetPassword(mobile, password, smsVerifyCode);
		return res == 1;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#registerUser(com.bsh.mybatis.model.User)
	 */
	@Override
	public User registerUser(User user) {
		int id= userDao.addUser(user);
		DesUtils des=new DesUtils();
		String password=des.encryptPassword(user.getPassword());
		user.setPassword(password);
		User newUser = userDao.selectByPrimaryKey(id);
		return newUser;
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#getUserByMobile(java.lang.String)
	 */
	@Override
	public User getUserByMobile(String mobile) {
		return userDao.selectByMobile(mobile);
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#getUserByName(java.lang.String)
	 */
	@Override
	public User getUserByName(String name) {
		return userDao.selectByName(name);
	}
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.UserService#checkUserByNameOrMobile(java.lang.String)
	 */
	@Override
	public User checkUserByNameOrMobile(String name){
		return userDao.selectByNameOrMobile(name);
	}

	@Override
	public Integer updateByPrimaryKey(User user) {
		return userDao.updateByPrimaryKey(user);
	}

	@Override
	public List<Map<String, Object>> getMyRecUserList(Pager pager,
			Integer userid) {
		return userDao.getMyRecUserList(pager, userid);
	}

	@Override
	public List<Map<String, Object>> getMyRecMerchantList(Pager pager,
			Integer userid) {
		return userDao.getMyRecMerchantList(pager, userid);
	}

	@Override
	public int getIpLoginTimes(String ip) {
		return userDao.getIpLoginTimes(ip);
	}

	@Override
	public List<Map<String, Object>> getBankPage(Pager pager,UserListQryDto qryParam) {
		StringBuilder where=new StringBuilder();
		if(qryParam!=null){
			if(qryParam.getBankId()!=null && qryParam.getBankId()!=-1){
				where.append(String.format(" and b.bankid= %d ",qryParam.getBankId()));
			}
			if(!StringUtils.isEmpty(qryParam.getName())){
				where.append(String.format(" and u.name='%s' ",qryParam.getName().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getRealName())){
				where.append(String.format(" and u.real_name like '%%%s%%' ",qryParam.getRealName().trim()));
			}
			if(!StringUtils.isEmpty(qryParam.getMobile())){
				where.append(String.format(" and u.mobile='%s' ",qryParam.getMobile().trim()));
			}
		}
		List<Map<String, Object>> list = userDao.getBankPage(pager,where.toString());
		return list;
	}	
	
}
